/**
 * Created by ITS Lab on 2015/11/15.
 */
window.onload= function () {
    waterfall('main', 'box');
    // var dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.png'},{'src':'4.jpg'}]};
}
    var txt;
    var data;
    var temp = 0;
    var kind_id = document.getElementById("3").value;
       var id=30;
    // console.log(kind_id);
    window.onscroll = function () {
        if (checkScrollSlide()) {
            var oParent = document.getElementById("main");
            var oBox = getByClass(oParent, "box");
            var request = new XMLHttpRequest();
            request.open("POST", url + "/Home/ThingLost/allajax", true);
            var s = "id=" + id;
            id=id+10;
            request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            request.send(s + "&kind_id=" + kind_id);
                request.onreadystatechange = function () {
                    if (request.readyState == 4 && request.status == 200) {
                        var txt = request.responseText;
                        var data = eval("(" + txt + ")");
                        for (var i = 0; i < data.length; i++) {
                            if (data[i].status != 2 && data[i].status != 3) {
                                var oBox = document.createElement("div");

                                oBox.className = "box";
                                oParent.appendChild(oBox);
                                var oA = document.createElement("a");
                                oA.href = url + "/Home/Information/index/id/" + data[i].id;
                                oBox.appendChild(oA);
                                var oPic = document.createElement("div");
                                oPic.className = "pic";
                                oA.appendChild(oPic);
                                var oImg = document.createElement("img");
                                if(data[i].pic1=='/')
                                oImg.src=url1+'/Public/Img/noPicture.jpg';
                                else
                                oImg.src = url1 + "/Public/" + data[i].pic1; 
                                oImg.setAttribute('onerror',"imgError(this)");

                           // oImg.addEventListener('error', imgError(oImg), true); 

                               //oImg.attachEvent("onerror",imgError(oImg));
                                var sPan = document.createElement("span");
                                sPan.innerHTML = data[i].name;
                                oPic.appendChild(oImg);
                                oPic.appendChild(sPan);
                                waterfall("main", "box");
                            }
                        }
                        waterfall("main", "box");
                    }
                }
        }
        waterfall("main", "box");

    }


function waterfall(parent,box)
{
        var oParent=document.getElementById(parent);
        var oBox=getByClass(oParent,box);
        //console.log(oBox);
        var oBoxW=oBox[0].offsetWidth;
        
        var col;
        // if(document.documentElement.clientWidth<400) {
        //col = 1;
        
        // }
        // else
        col =Math.floor(document.documentElement.clientWidth/oBoxW);
        var i;
    for(i=0;i<col;i++)
        oBox[i].style.cssText='padding-top:55px';
    //设置main的宽度
    oParent.style.cssText='width:'+oBoxW*col+'px;margin:0 auto';

    var hArr=new Array();

    for(var i=0;i<oBox.length;i++)
    {
        if(i<col)
        {
            hArr.push(oBox[i].offsetHeight);
            oBox[i].style.position="absolute";
            oBox[i].style.left=oBoxW*i+"px";

        }
        else{
            var minH=Math.min.apply(null,hArr);
            var index=getMinHIndex(hArr,minH);
            oBox[i].style.position="absolute";
            oBox[i].style.top=minH+"px";
            oBox[i].style.left=oBoxW*index+"px";
            hArr[index]=hArr[index]+oBox[i].offsetHeight;
        }
    }
   // console.log(hArr);
}
function getMinHIndex(Array,value)
{
    var i;
    for(i=0;i<Array.length;i++)
    {
        if(Array[i]==value)
            return i;
    }
}
function getByClass(parent,clsName)
{
var boxArr=new Array();//用来存储获得到的所有class为box的元素
    oElements=parent.getElementsByTagName("*");
    var i;
    for(i=0;i<oElements.length;i++)
    {
        if(oElements[i].className==clsName)
         boxArr.push(oElements[i]);
    }
return boxArr;
}
function checkScrollSlide()
{
    var oParent=document.getElementById("main");
    var oBox=getByClass(oParent,'box');
    var lastBoxH=oBox[oBox.length-1].offsetTop+Math.floor(oBox[oBox.length-1].offsetHeight)*1/100000;
    var scrollTop=document.body.scrollTop||document.documentElement.scrollTop;
    var height=document.body.clientHeight||document.documentElement.clientHeight;
    if(lastBoxH<scrollTop+height)
    return true;
    else
    return false;
}

